<?php

mb_internal_encoding('UTF-8');

// Find out where we're installed
$app_root = realpath(dirname(dirname(dirname(__FILE__))));

// Add the include path 
ini_set('include_path', $app_root . '/includes:' . $app_root . '/admin/includes:' . ini_get('include_path'));

// Force UTC
date_default_timezone_set('UTC');

// Get the app config
require $app_root . '/config.php';

// DB connect
try {
	$db = Zend_Db::factory($cms_config['db_factory'], array(
	    'host'     => $cms_config['db_host'],
	    'username' => $cms_config['db_user'],
	    'password' => $cms_config['db_pass'],
	    'dbname'   => $cms_config['db_database']
	));
    $db->getConnection();
	$db->query("SET NAMES 'utf8'");
	$db->query("SET time_zone = '+00:00'");
	Store::getInstance()->db = $db;
} catch (Zend_Exception $e) {
	header('HTTP/1.1 503 Service Unavailable');
	header('Retry-after: 600');
	echo "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n";
	echo "<html><head>\n";
	echo "<title>This site is temporarily unavailable.</title>\n";
	echo "</head><body>\n";
	echo "<h1>This site is temporarily unavailable.</h1>\n";
	echo "<p>Please try your request again.</p>\n";
	echo "<!-- " . $e->getMessage() . " -->\n";
	echo "<hr>\n";
	echo "<address>".$_SERVER['SERVER_SOFTWARE']." at ".$_SERVER['SERVER_NAME']."</address>\n";
	echo "</body></html>\n";
	exit;
}

/*
 * Assorted generic functions that should really already be
 * in PHP.
 *
 */
require 'functions.php';

/*
 * Load Smarty. Needed in initialise for emails.
 *
 */
require "Smarty/Smarty.class.php";
$smarty = new Webtaculous_Smarty;
Store::getInstance()->smarty = $smarty;
$smarty->compile_dir = $app_root . '/admin/templates_c';
if ($cms_config['smarty_use_sub_dirs']) $smarty->use_sub_dirs = true;
$smarty->assign('cms_name', $cms_config['cms_name']);
$smarty->assign('cms_domain', $cms_config['cms_domain']);
require 'modifier.date.php';
require 'resource.string.php';


/*
 * Class autoloader - currently just configured for Zend
 * style.  Replaces _ with / and adds .php
 *
 */
function __autoload($class) {
	$class = str_replace('_', '/', $class) . '.php';
	require_once($class);
}
